<?php 
namespace app\admin\controller;
use think\Db;//导入数据库类 (DB类)
use think\Controller;
use think\Session;
use think\Request;
use app\admin\model\User;
class Userlr extends Controller
{
    // public function login()
    // {

    //     return view('index/admin-login');
    // }
    public function loginP()
    {
        // 查询数据
        if(Request::instance()->isPost()){
            $user = db('admin')->where( 'name',input('username'))->find();
            $pwd = db('admin')->where('id',$user['id'])->where('pwd',input('userpwd'))->find();
           
            // 判断是否登录成功,并保存session
            if ($user && $pwd) {
                Session::set('admin',$user);
               
                $list = db('node')->field('mname,aname')->where('id in'.db('role_node')->field('nid')->where("rid in ".db('admin_role')->field('rid')->where('uid',$user['id'])->buildSql())->buildSql())->select();
                  
                    //控制器名转换为大写
                    foreach ($list as $key => $val) {
                        $list[$key]['mname'] = ucfirst($val['mname']);
                    }

                    //查看查询出来的信息
                    // V($list); exit;

                    $nodelist = array();
                    $nodelist['Index'] = array('index');
                    //遍历重新拼装
                    foreach($list as $v){
                        $nodelist[$v['mname']][] = $v['aname'];
                        //把修改和执行修改 添加和执行添加 拼装到一起
                        if($v['aname']=="edit"){
                            $nodelist[$v['mname']][]="save";
                        }
                        if($v['aname']=="add"){
                            $nodelist[$v['mname']][]="doadd";
                        }
                    }
                   
                    //将权限信息放置到session中
                    Session::set('admin.nodelist',$nodelist);
                    $this->redirect('admin/Home/index');
               

            }else{
                $this->error('账户或密码不正确');
                
            }
         }
    }

    //忘记密码
    public function find(){
        return view('index/find');
    }

    //验证手机号是否存在
    public function tel(){
        $tel=input('tel');
         
        $data=db('admin')->where('num',$tel)->find();

        if (empty($data)) {
            return json('用户不存在');
        }
       
    }
    //验证码发送
    //return 
    public function yzm(){
        $tel=input('tel');
         
        $data=db('admin')->where('num',$tel)->find();

        if (empty($data)) {
            return json('用户不存在');
        }else{
            $id=$data['id'];
        }
        $code=mt_rand(1000,9999);

        Session::set('code',$code);
        $result=sendTemplateSMS($tel,array($code,'5'),'1');
        
            if ($result['status']==0){
                 return $code;
            }else{
               return 'error';
            }
    }


    //验证码验证
    public function reset(){
        $tel=input('num');
        $yzm=input('yzm');

        // dump($yzm);dump($tel);exit;
        if (empty($tel&&$yzm)) {
            $this->error('手机号或验证码不能为空!');
        }
        $data=db('admin')->where('num',$tel)->find();

       if ($yzm==input('session.code')) {
            $this->assign('data',$data);
            return view('index/reset');
       }else{
            $this->error('验证码错误');
       }
    }

    //重设密码
    public function pwd()
    {   
        $id=input('id');
        $pwd=input('pwd1');
        $repwd=input('repwd');
        // dump($pwd);
        // dump($repwd);exit;
        if (empty($pwd&&$repwd)) {
          $this->error('密码不能为空');
        }

        if ($pwd!=$repwd) {
           $this->error('两次密码不一致');
        }
        $result=db('admin')->where('id',$id)->update(['pwd' =>$pwd]);
        if ($result>0) {
            $this->success('修改成功','index/index');
        }
    }
    // ===================退出登录=======================
    public function loginO()
    {
        Session::clear();
        $this->redirect('/admin');
    }
}